﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class InternalNews : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "InternalNews";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Tin theo nhóm");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }

        public override bool IsMainWebPart
        {
            get
            {
                return true;
            }
        }

        public override bool RunStandalone
        {
            get
            {
                return true;
            }
        }
        public override IWebPartConfig WebConfig
        {
            get
            {
                return new InputNewsConfig();
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            try
            {

                AjaxPro.Utility.RegisterTypeForAjax(typeof(InternalNews));
                Environment.Environments.CheckSessionTimeOut(true);

                
                string gfItemId = (string)Environments.Request("gfItemId");
                string XmlConfigItem = Environments.GetXmlWebPartConfigItem(gfItemId);
                if (XmlConfigItem == null)
                {
                    throw new Exception(ResourceBLL.GetText(WebPartId, "NotConfig", "Chưa cấu hình webpart này trong phần chức năng theo nhóm menu!"));
                }
                DataSet ds = new DataSet();
                ds.ReadXml(new System.IO.StringReader(XmlConfigItem));
                string GroupNewsId = (string)ds.Tables[0].Rows[0]["GroupNewsId"];
                string GroupNewsItemId = (string)ds.Tables[0].Rows[0]["GroupNewsItemId"];
                GroupNewsCls GroupNewsCls = new GroupNewsBLL().CreateModel(GroupNewsId);
                if (GroupNewsCls == null)
                {
                    return Environment.Environments.GetRecordNotFound();
                }
                GroupNewsItemCls GroupNewsItemCls = new GroupNewsBLL().CreateModelItem(Environments.ActiveLanguage, GroupNewsItemId);

                //PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocument");
                //Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "ShowItems", GroupNewsCls.GroupNewsId, GroupNewsCls.Owner, true);
                string RetHtml =
                    "<input type=hidden id=txtGroupNewsId value=\""+GroupNewsId+"\">"+
                    WebPartUtilities.AttachJavascript(WebPartId, "InternalNews.js") +
                    "<div class=cssHeading>" + GroupNewsCls.Subject + " / "+GroupNewsItemCls.GroupNewsItemName+"</div>" +
                    "<div id=divProcessing></div>" +
                    "<div id=divFormResult>" + Search(GroupNewsId,GroupNewsItemId, 0) + "</div>";
                return RetHtml;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        
        [AjaxPro.AjaxMethod]
        public string Search(string GroupNewsId,string GroupNewsItemId, int CurrentPageIndex)//, string Code, string Subject, string LawDocumentSignerId, string LawDocumentTypeId, string LawDocumentOfficeId,string Keyword, int CurrentPageIndex)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                GroupNewsCls GroupNewsCls = new GroupNewsBLL().CreateModel(GroupNewsId);
                //PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocument");
                //Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "ShowItems", GroupNewsCls.GroupNewsId, GroupNewsCls.Owner, true);

                DataTable dtItems = new GroupNewsBLL().ReadingNewsByGroupNewsItem(GroupNewsItemId);
                int StartIndex = 0;
                int EndIndex = 0;
                string PagingText = new PagingCls().GetPaging(GroupNewsItemId, dtItems.Rows.Count, 25, 10, CurrentPageIndex, "JumpPageInternalNews", ref StartIndex, ref EndIndex);

                string Html =
                    "<div class=cssPagingInternalNews>" + PagingText + "</div>" +
                    "<input type=hidden id=txtPageIndex value=\"" + CurrentPageIndex + "\">" +
                    "<div class=cssHeading>" + ResourceBLL.GetText(WebPartId, "TotalNews", "Tổng số tin") + ": (" + dtItems.Rows.Count.ToString("#,##0") + ")</div>";

                if (dtItems.Rows.Count == 0)
                {
                    Html += "<div class=\"cssNoData\">" + ResourceBLL.GetText(WebPartId, "NoData", "Chưa có dữ liệu nào trong chuyên mục này!") + "</div>";
                }
                else
                {
                    Html +=
                     "<table class=cssInternalNews>";
                    for (int iIndex = StartIndex; iIndex < EndIndex; iIndex++)
                    {
                        string NewsId = (string)dtItems.Rows[iIndex]["NewsId"];
                        string Subject = (string)dtItems.Rows[iIndex][Environments.ActiveLanguage + "Subject"];
                        string VieShortContent = (string)dtItems.Rows[iIndex][Environments.ActiveLanguage + "ShortContent"];
                        string Source = "";
                        if (dtItems.Rows[iIndex]["Source"] != DBNull.Value)
                        {
                            Source = (string)dtItems.Rows[iIndex]["Source"];
                        }
                        Source = Source.Trim();
                        string LargeImage = "";
                        try
                        {
                            LargeImage = (string)dtItems.Rows[iIndex]["ImageUrl"];
                        }
                        catch { }
                        string ImageUrl = Environments.HttpMediaRoot + "/media/" + Environments.PortalAlias + "/news/images/" + Environments.ActiveLanguage + "/" + LargeImage;
                        //string PathImage = Environments.WorkingPath + "\\media\\" + Environments.PortalAlias + "\\news\\images\\" + Environments.ActiveLanguage + "\\" + LargeImage;
                        string Url = Screen.BuildUrl(new InternalViewNews().WebPartId, new ParamCls[]{
                        new ParamCls("NewsId",NewsId)
                    });
                        DateTime EntryDate = (DateTime)dtItems.Rows[iIndex]["EntryDate"];
                        bool Ok = false;
                        if (LargeImage.Trim().Length > 0) Ok = true;
                        //if (Ok)
                        //{
                        //    Ok = new System.IO.FileInfo(PathImage).Exists;
                        //}
                        Html += "<tr>";
                        if (Ok)
                        {
                            Html +=
                                "     <td valign=top class=cssTdImageInternal><img class=cssImageInternalNews src=\"" + ImageUrl + "\"></td>";
                        }
                        if (Ok)
                        {
                            Html +=
                                    "     <td valign=top>";
                        }
                        else
                        {
                            Html +=
                                    "     <td valign=top colspan=2>";
                        }
                        Html +=
                                "           <table>" +
                                "               <tr>" +
                                "                   <td><span class=cssInternalNewsSubject>" + (iIndex + 1) + ". <a href=\"" + Url + "\">" + Subject + "</a> - " + EntryDate.ToString("dd/MM/yyyy HH:mm") + "</subject></td>" +
                                "               </tr>" +
                                "               <tr>" +
                                "                   <td><span class=cssInternalNewsShortContent>" + VieShortContent + "</span></td>" +
                                "               </tr>";

                        if (Source.Trim().Length > 0)
                        {
                            Html +=
                                    "               <tr>" +
                                    "                   <td><span class=cssInternalNewsSource>" + ResourceBLL.GetText(WebPartId, "Source", "Nguồn tin") + ": " + Source + "</span></td>" +
                                    "               </tr>";
                        }
                        Html +=
                                "           </table>" +
                                "     </td>" +
                                "</tr>";
                    }
                    Html +=
                        "</table>";
                }
                
                return Html;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            string Url = Environment.Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
            return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang đọc dữ liệu...") + "</a>";
        }
    }
}